package com.test;


/******************************************************************************
 * 演示使用POI 读取 Excel 2007
 * 
 * 关键字：Java Excel POI POI-HSSF POI-XSSF
 * 
 * 作者：高宏伟(DukeJoe)
 * 时间：2009-10-22 16:12:25
 * 地点：黑龙江省哈尔滨市道里区通达街291号
 * 注释：http://blog.donews.com/dukejoe/archive/2009/10/22/1567379.aspx
 * 
 * 开发环境详细说明：
 * 1、java version "1.6.0_14"
 * 2、Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
 * 3、Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)
 * 4、Microsoft Excel 2007
 * 5、Windows XP Home Edition Service Pack 3
 * 6、Apache POI 3.5
 * 
 *****************************************************************************/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class POIExcelReadDemo1
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        try
        {
        InputStream inp;
            inp = new FileInputStream("gaohw.xlsx");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            for (Iterator rit = sheet.rowIterator(); rit.hasNext();)
            {
                // 迭代行
                Row row = (Row) rit.next();
                // 迭代单元格
                for (Iterator cit = row.cellIterator(); cit.hasNext();)
                {
                    Cell cell = (Cell) cit.next();
                    // 开始操作单元格
                    // 在每一行的输出都打印如 "5:6 例子字符串"，5:6代表第5行，第6列
                    // 注意行和列是基于0索引的
                    System.out.print(cell.getRowIndex() + ":" + cell.getColumnIndex()
                            + " ");
                    // 打印单元格内的数据
                    switch (cell.getCellType())
                    {
                    case Cell.CELL_TYPE_STRING:
                        System.out.println(cell.getRichStringCellValue().getString());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell))
                        {
                            System.out.println(cell.getDateCellValue());
                        } else
                        {
                            System.out.println(cell.getNumericCellValue());
                        }
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(cell.getBooleanCellValue());
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        System.out.println(cell.getCellFormula());
                        break;
                    default:
                        System.out.println();
                    }
                }
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (InvalidFormatException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
 

